package com.jieli.bt.att.ui.device

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothProfile
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jieli.bt.att.R
import com.jieli.bt.att.data.device.DeviceConnection
import com.jieli.bt.att.tool.ble.model.ScanDeviceInfo
import com.jieli.bt.att.util.BluetoothUtil
import com.jieli.bt.att.util.gone
import com.jieli.bt.att.util.show
import com.wang.avi.AVLoadingIndicatorView

/**
 * ScanDeviceAdapter
 * @author zqjasonZhong
 * @since 2025/3/27
 * @email zhongzhuocheng@zh-jieli.com
 * @desc 搜索设备适配器
 */
class ScanDeviceAdapter : BaseQuickAdapter<ScanDeviceInfo, BaseViewHolder>(R.layout.item_device) {

    override fun convert(holder: BaseViewHolder, item: ScanDeviceInfo) {
        holder.setText(R.id.tv_device_name, BluetoothUtil.getDeviceName(item.device, false))
        holder.setText(
            R.id.tv_device_msg,
            BluetoothUtil.formatString("%s\tRSSI: %d", item.device.address, item.rssi)
        )
        holder.getView<AVLoadingIndicatorView>(R.id.aiv_loading).also {
            if (item.connection == BluetoothProfile.STATE_CONNECTING) {
                it.visibility = View.VISIBLE
                it.show()
            } else {
                it.hide()
                it.gone()
            }
        }
        holder.getView<View>(R.id.view_line).show()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun addData(data: ScanDeviceInfo) {
        if (data.connection == BluetoothProfile.STATE_CONNECTED) return
        val index = this.data.indexOf(data)
        if (index == -1) {
            super.addData(data)
        } else {
            val item = getItem(index)
            item.rssi = data.rssi
            item.rawData = data.rawData
            item.connection = data.connection
            item.isEnableConnect = data.isEnableConnect
            notifyItemChanged(index)
        }
        /*if (this.data.contains(data)) {
            this.data.remove(data)
        }
        super.addData(data)
        this.data.sortByDescending {
            it.rssi
        }
        notifyDataSetChanged()*/
    }

    @SuppressLint("NotifyDataSetChanged")
    fun updateItem(connection: DeviceConnection) {
        val scanDevice = findScanDevice(connection.device) ?: return
        scanDevice.connection = connection.state
        if (scanDevice.connection == BluetoothProfile.STATE_CONNECTED) {
            remove(scanDevice)
        } else {
            notifyItemChanged(getItemPosition(scanDevice))
        }
    }

    private fun isLastItem(item: ScanDeviceInfo): Boolean {
        return getItemPosition(item) == data.size - 1
    }

    private fun findScanDevice(device: BluetoothDevice): ScanDeviceInfo? {
        if (data.isEmpty()) return null
        for (scanDevice in data) {
            if (BluetoothUtil.deviceEquals(device, scanDevice.device)) {
                return scanDevice
            }
        }
        return null
    }
}